﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VisualAgile.IBLL;
using VisualAgile.Model;
using System.Collections;

namespace VisualAgile.Web.Tests.Fakes
{
    public class FakeReleaseManager : IReleaseManager   
    {
        private List<Release> releaseList;

        public FakeReleaseManager(List<Release> releases)
        {
            releaseList = releases;
        }

        /// <summary>
        /// Create a release (back to contain the default value)
        /// </summary>
        /// <returns></returns>
        public Release NewRelease()
        {
            var release = new Release();
                        
            release.CreateTime = DateTime.Now;
            release.Name = "New Release"; // hard coded default release text
            release.PeriodFrom = DateTime.Now;
            release.PeriodTo = DateTime.Now.AddDays(27);

            return release;
        }

        /// <summary>
        /// Save release
        /// </summary>
        /// <param name="release">Release entity</param>
        /// <returns></returns>
        public Release Save(Release release)
        {
            Validate(release);
            if (Get(release.ReleaseID) != null)
                throw new Exception("Existing release");
            release.ReleaseID = Guid.NewGuid();
            releaseList.Add(release);

            return Get(release.ReleaseID);
        }

        /// <summary>
        /// Access to releases
        /// </summary>
        /// <param name="releaseId">Release ID</param>
        /// <returns></returns>
        public Release Get(Guid releaseId)
        {
            return releaseList.SingleOrDefault(u => u.ReleaseID == releaseId);
        }

        /// <summary>
        /// Access to releases
        /// </summary>
        /// <param name="releaseId">Release ID</param>
        /// <returns></returns>
        public IList GetByProject(Guid projectId)
        {
            return releaseList.FindAll(r => r.ProjectID == projectId);
        }      

        /// <summary>
        /// Delete Release
        /// </summary>
        /// <param name="releaseId">Release ID</param>
        /// <returns></returns>
        public void Delete(Guid releaseId)
        {
            var release = Get(releaseId);
            if (release == null) throw new Exception("NotFound");
            releaseList.Remove(release);
        }

        /// <summary>
        /// Update Release
        /// </summary>
        /// <param name="release">Release Entity</param>
        /// <returns></returns>
        public Release Update(Release release)
        {
            return Get(release.ReleaseID);
        }

        /// <summary>
        /// Access to releases
        /// </summary>
        /// <returns></returns>
        public IList GetAll()
        {
            return releaseList;
        }

        private void Validate(Release release)
        {
            //TODO throw exception on error.
        }        
    }
}
